package com.tamtamy.jtta.data.json;

import java.util.ArrayList;
import java.util.Iterator;

import net.sf.json.JSONObject;

import com.tamtamy.jtta.data.Tag;
import com.tamtamy.jtta.data.TagList;
import com.tamtamy.jtta.utility.JSONUtility;

public class JSONTagList extends TagList{
	
	private static final long serialVersionUID = -5822965194917847942L;

	public JSONTagList(JSONObject jsonObject, String fieldName) {
		super(getTagsListFromStringArray(jsonObject, fieldName));
	}
	
	public JSONTagList(JSONObject jsonObject) {
		super(getTagsListFromStringArray(jsonObject, "list"));
	}
	
	public static ArrayList<Tag> getTagsListFromStringArray(JSONObject jsonObject, String fieldName) {
		ArrayList<Tag> tagsList = new ArrayList<Tag>();
		ArrayList<String> tagsStringList = JSONUtility.getStringList(jsonObject, fieldName);
		for (Iterator<String> iterator = tagsStringList.iterator(); iterator.hasNext();) {
			String tagName = (String) iterator.next();
			Tag tag = new Tag(tagName);
			tagsList.add(tag);
		}
		return tagsList;
	}
}
